-- Copyright (c) 2016 Lucky Byte, Inc.
--
-- Summary : 消费代付交易匹配列表
--
-- Author : xiaohu <xiaohu@lucky-byte.com>
-- Since  : 2016.6.22
--
-- Reference
--  006.3 - 中国银联银行卡系统技术规范 第三部分 文件接口规范
--
-- History
--  2016.06.22 : 建立此文件
--

-- 生成报表
--
-- shared_vars: 公共数据
--
local _run = function(shared_vars)
    -- 查询所有 POS 009.1 渠道的成功交易
    -- 当前只有消费交易
    local _table = jdbc.select([[
        select * from pay_trlist
            where status = 0 and incoming = false and settl = true and
                chnl = 1 and
                json_extract_path_text(o_pkt, '39') = '00' and
                json_extract_path_text(o_pkt, '15') = ?
    ]], shared_vars.settl_date)
    logger.info('消费交易数: %s', _table.count)

    local _chnl_1_trlist = _table.to_table()
    for _, v in ipairs(_chnl_1_trlist) do
        v.i_pkt = core.json.decode(v.get('i_pkt'))
        v.o_pkt = core.json.decode(v.get('o_pkt'))
    end

    -- 查询所有 ACP 成功交易
    -- 当前只有代付交易
    local _table = jdbc.select([[
        select t.*, json_extract_path_text(t2.i_pkt, '48') as f48
            from pay_trlist as t
            left join pay_trlist as t2 on
                t2.uuid = t.uuid and t2.incoming = true
            where t.status = 0 and t.incoming = false and t.settl = true and
                t.chnl = 3 and t.n_pkt is not null and
                json_extract_path_text(t.n_pkt, 'respCode') = '00' and
                json_extract_path_text(t.n_pkt, 'settleDate') = ?
    ]], shared_vars.settl_date)
    logger.info('代付交易数: %s', _table.count)

    local _chnl_3_trlist = _table.to_table()
    for _, v in ipairs(_chnl_3_trlist) do
        v.i_pkt = core.json.decode(v.get('i_pkt'))
        v.o_pkt = core.json.decode(v.get('o_pkt'))
        v.n_pkt = core.json.decode(v.get('n_pkt'))
    end

    local _split_f48 = function(_f48)
        local _result = {}
        _result.merno    = string.sub(_f48, 79, 93)
        _result.termid   = string.sub(_f48, 94, 101)
        _result.tracenum = string.sub(_f48, 108, 113)
        _result.retrefno = string.sub(_f48, 120, 131)
        _result.tramt    = string.sub(_f48, 155, 166)
        return _result
    end

    local _matched_list = {}

    -- 匹配消费与代付交易
    for _, v3 in ipairs(_chnl_3_trlist) do
        local _f48 = v3.get('f48')
        if string.trimlen(_f48) > 0 then
            _f48_fields = _split_f48(_f48)
            for _, v1 in ipairs(_chnl_1_trlist) do
                local _i_pkt = core.json.decode(v1.get('i_pkt'))
                local _o_pkt = core.json.decode(v1.get('o_pkt'))
                if  v1.i_pkt['4']  == _f48_fields.tramt and
                    v1.i_pkt['11'] == _f48_fields.tracenum and
                    v1.i_pkt['41'] == _f48_fields.termid and
                    v1.i_pkt['42'] == _f48_fields.merno and
                    v1.o_pkt['37'] == _f48_fields.retrefno then
                    v1.matched = true
                    v3.matched = true
                    table.insert(_matched_list, {v1 = v1, v3 = v3})
                    break
               end
            end
        end
    end

    -- 查询未匹配代付记录
    for _, v3 in ipairs(_chnl_3_trlist) do
        if not v3.matched then
            table.insert(_matched_list, {v3 = v3})
        end
    end

    -- 查询未匹配消费记录
    for _, v1 in ipairs(_chnl_1_trlist) do
        if not v1.matched then
            table.insert(_matched_list, {v1 = v1})
        end
    end

    -- 写文件
    local _f1 = io.open(shared_vars.save_path .. '/report01_1.txt', 'w')
    local _f2 = io.open(shared_vars.save_path .. '/report01_2.txt', 'w')
    local _f3 = io.open(shared_vars.save_path .. '/report01_3.txt', 'w')

    for _, v in ipairs(_matched_list) do
        local _v1_fields = ",,,,,,,"
        local _v3_fields = ",,,,,"
        if v.v1 then
            _v1_fields = v.v1.i_pkt['42'] .. ','
            _v1_fields = _v1_fields .. v.v1.i_pkt['41'] .. ','
            _v1_fields = _v1_fields .. v.v1.i_pkt['11'] .. ','
            _v1_fields = _v1_fields .. v.v1.i_pkt['4'] .. ','
            _v1_fields = _v1_fields .. v.v1.o_pkt['13'] .. ','
            _v1_fields = _v1_fields .. v.v1.o_pkt['12'] .. ','
            _v1_fields = _v1_fields .. v.v1.o_pkt['37'] .. ','
            _v1_fields = _v1_fields .. v.v1.o_pkt['39']
        end
        if v.v3 then
            _v3_fields = v.v3.i_pkt.merId .. ','
            _v3_fields = _v3_fields .. v.v3.i_pkt.orderId .. ','
            _v3_fields = _v3_fields .. v.v3.i_pkt.txnAmt .. ','
            _v3_fields = _v3_fields .. v.v3.n_pkt.queryId .. ','
            _v3_fields = _v3_fields .. v.v3.n_pkt.traceNo .. ','
            _v3_fields = _v3_fields .. v.v3.n_pkt.respCode
        end
        _f1:write(_v1_fields .. ',' .. _v3_fields .. '\n')
        _f1:write(_v3_fields .. ',' .. _v1_fields .. '\n')
        _f2:write(_v1_fields .. ',' .. _v3_fields .. '\n')
        _f3:write(_v3_fields .. ',' .. _v1_fields .. '\n')
    end

    _f1:close()
    _f2:close()
    _f3:close()
end


return {
    run = _run
}
